package com.blog;


import com.blog.vo.RTXCard;
import org.apache.http.Header;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.*;

/**
 * @author cgx
 * @Date 2021/12/23 11:53
 */
@SpringBootTest
public class JD_3060ti_OR_3060_QueryPrice {

    /**
     * 本类用于爬取京东3060ti和3060旗舰店的价格
     * 包含(华硕、微星、七彩虹、影驰、索泰、铭瑄，其他的目前先不爬取)
     * @param
     * @return
     */

    //是否导出excel，默认否
    private boolean ExportExcel = false;

    private static String RTX3060Url = "https://search.jd.com/search?keyword=3060&wq=3060&pvid=cfe82424efab4be9abbc09eba766b543&ev=exbrand_%E6%8A%80%E5%98%89%EF%BC%88GIGABYTE%EF%BC%89%7C%7C%E5%8D%8E%E7%A1%95%EF%BC%88ASUS%EF%BC%89%7C%7C%E4%B8%83%E5%BD%A9%E8%99%B9%EF%BC%88Colorful%EF%BC%89%7C%7C%E5%BD%B1%E9%A9%B0%7C%7C%E5%BE%AE%E6%98%9F%EF%BC%88MSI%EF%BC%89%7C%7C%E7%B4%A2%E6%B3%B0%EF%BC%88ZOTAC%EF%BC%89%7C%7C%E9%93%AD%E7%91%84%EF%BC%88MAXSUN%EF%BC%89%5E#J_searchWrap";
    private static String RTX3060tiUrl = "https://search.jd.com/search?keyword=3060ti&wq=3060&pvid=cfe82424efab4be9abbc09eba766b543&ev=exbrand_%E6%8A%80%E5%98%89%EF%BC%88GIGABYTE%EF%BC%89%7C%7C%E5%8D%8E%E7%A1%95%EF%BC%88ASUS%EF%BC%89%7C%7C%E4%B8%83%E5%BD%A9%E8%99%B9%EF%BC%88Colorful%EF%BC%89%7C%7C%E5%BD%B1%E9%A9%B0%7C%7C%E5%BE%AE%E6%98%9F%EF%BC%88MSI%EF%BC%89%7C%7C%E7%B4%A2%E6%B3%B0%EF%BC%88ZOTAC%EF%BC%89%7C%7C%E9%93%AD%E7%91%84%EF%BC%88MAXSUN%EF%BC%89%5E#J_searchWrap";


    @Test
    void test1() throws IOException {
        Document document3060 = Jsoup.connect(RTX3060Url).timeout(5000).get();
        Document document3060ti = Jsoup.connect(RTX3060tiUrl).timeout(5000).get();
        //获取商品列表
        Elements RTX3060 = document3060.body().getElementById("J_goodsList").getElementsByClass("gl-warp");
        Elements RTX3060ti = document3060ti.body().getElementById("J_goodsList").getElementsByClass("gl-warp");

        List<RTXCard> Card3060List = new ArrayList<>();
        List<RTXCard> Card3060tiList = new ArrayList<>();

        for (int i = 0; i < RTX3060.size(); i++) {
            Element currentElement = RTX3060.get(i);
            //获取前三十个商品
            Elements goods = currentElement.getElementsByClass("gl-item");
            for (int j = 0; j < goods.size(); j++) {
                //遍历每个商品
                Element element = goods.get(j);

                Elements elementsByClass = element.getElementsByClass("gl-i-wrap");//商品div
                String shopName = elementsByClass.get(0).getElementsByClass("curr-shop").get(0).html();
                String price = elementsByClass.get(0).getElementsByClass("p-price").get(0).getElementsByTag("i").get(0).html();
                String goodsUrl = elementsByClass.get(0).getElementsByClass("p-name").get(0).getElementsByTag("a").get(0).attr("href");
                if (shopName.contains("旗舰店")  && shopName.contains("自营")){
                    RTXCard rtxCard = new RTXCard();
                    rtxCard.setShopName(shopName);
                    rtxCard.setPrice(price);
                    rtxCard.setSign("3060");
                    rtxCard.setGoodsUrl(goodsUrl);
                    Card3060List.add(rtxCard);
                }

            }
        }

        for (int i = 0; i < RTX3060ti.size(); i++) {
            Element currentElement = RTX3060ti.get(i);
            //获取前三十个商品
            Elements goods = currentElement.getElementsByClass("gl-item");
            for (int j = 0; j < goods.size(); j++) {
                //遍历每个商品
                Element element = goods.get(j);

                Elements elementsByClass = element.getElementsByClass("gl-i-wrap");//商品div
                String shopName = elementsByClass.get(0).getElementsByClass("curr-shop").get(0).html();
                String price = elementsByClass.get(0).getElementsByClass("p-price").get(0).getElementsByTag("i").get(0).html();
                String goodsUrl = elementsByClass.get(0).getElementsByClass("p-name").get(0).getElementsByTag("a").get(0).attr("href");
                if (shopName.contains("旗舰店")  && shopName.contains("自营")){
                    RTXCard rtxCard = new RTXCard();
                    rtxCard.setShopName(shopName);
                    rtxCard.setPrice(price);
                    rtxCard.setSign("3060");
                    rtxCard.setGoodsUrl(goodsUrl);
                    Card3060tiList.add(rtxCard);
                }

            }
        }

        System.err.println("3060价格如下");
        for (int i = 0; i < Card3060List.size(); i++) {
            System.err.println(Card3060List.get(i).getShopName() +" : "+Card3060List.get(i).getPrice() + " 链接 "+Card3060List.get(i).getGoodsUrl());
        }

        System.err.println("3060ti价格如下");
        for (int i = 0; i < Card3060tiList.size(); i++) {
            System.err.println(Card3060tiList.get(i).getShopName() +" : "+Card3060tiList.get(i).getPrice() + " 链接 "+Card3060tiList.get(i).getGoodsUrl());
        }

        //是否导出文件
        if (ExportExcel){
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet3060 = workbook.createSheet("3060");
            HSSFSheet sheet3060ti = workbook.createSheet("3060ti");

            String[] hearder = {"店铺名","价格","商品链接"};
            
            //3060标题
            HSSFRow row1 = sheet3060.createRow(0);
            for (int i = 0; i < hearder.length; i++) {
                HSSFCell cell = row1.createCell(i);
                cell.setCellValue(hearder[i]);
            }
            //3060数据
            for (int i = 0; i < Card3060List.size(); i++) {
                
            }
            
        }


    }


    @Test
    void test2() throws IOException {
        Document document = Jsoup.connect(RTX3060tiUrl).timeout(5000).get();
        //获取商品列表
        Elements warp = document.body().getElementById("J_goodsList").getElementsByClass("gl-warp");

        List<RTXCard> Card3060tiList = new ArrayList<>();

        for (int i = 0; i < warp.size(); i++) {
            Element currentElement = warp.get(i);
            //获取前三十个商品
            Elements goods = currentElement.getElementsByClass("gl-item");
            for (int j = 0; j < goods.size(); j++) {
                //遍历每个商品
                Element element = goods.get(j);

                Elements elementsByClass = element.getElementsByClass("gl-i-wrap");//商品div
                String shopName = elementsByClass.get(0).getElementsByClass("curr-shop").get(0).html();
                String price = elementsByClass.get(0).getElementsByClass("p-price").get(0).getElementsByTag("i").get(0).html();
                String goodsUrl = elementsByClass.get(0).getElementsByClass("p-name").get(0).getElementsByTag("a").get(0).attr("href");
                if (shopName.contains("旗舰店")  && shopName.contains("自营")){
                    RTXCard rtxCard = new RTXCard();
                    rtxCard.setShopName(shopName);
                    rtxCard.setPrice(price);
                    rtxCard.setSign("3060");
                    rtxCard.setGoodsUrl(goodsUrl);
                    Card3060tiList.add(rtxCard);
                }

            }
        }
        System.err.println("3060ti价格如下");
        for (int i = 0; i < Card3060tiList.size(); i++) {
            System.err.println(Card3060tiList.get(i).getShopName() +" : "+Card3060tiList.get(i).getPrice() + " 链接 "+Card3060tiList.get(i).getGoodsUrl());
        }
    }


}
